Skip to content

feat(core): Enrich MCP SDK spans instead of duplicating them - #26309

Draft
sentry-junior[bot] wants to merge 6 commits into
developfrom
feat/mcp-sdk-otel-handoff
Draft

sentry-junior[bot] wants to merge 6 commits into
developfrom
feat/mcp-sdk-otel-handoff

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Why: The next @modelcontextprotocol/server release starts its own OpenTelemetry span for every MCP request and notification whenever a global tracer provider is registered (modelcontextprotocol/typescript-sdk#3115). With the Sentry tracer provider registered, every request gets two nested server spans with the same name. That happens by default in nextjs, sveltekit, deno and vercel-edge, and anywhere enableOpenTelemetrySetup: true is set. This change adds our attributes to the span the MCP SDK starts. We only start our own span when the SDK records nothing. Upstream's docs tell Sentry users to remove the McpServer integration instead, but that loses tool arguments and results, which upstream deliberately doesn't record.

Non-obvious decisions:

  • The hook goes through Protocol._instrumentation. That field is package-internal, but the integration already reads _registeredTools and similar. Without the field (v1, v2 ≤ 2.4, openTelemetry: false), nothing changes.
  • We only hand off when the OpenTelemetry API registry has a tracer provider. Without one (the @sentry/node default), the existing code path runs unchanged. If a non-Sentry provider is registered, the SDK's span never reaches Sentry, so we start our own span inside the SDK's scope.
  • The SDK ends its span before transport.send. Response attributes (tool/prompt results, initialize server info) are therefore applied from the SDK's end callback, not from send-time correlation.

How to review: Start with upstreamInstrumentation.ts, then the hand-off branches in transport.ts. The changes in spans.ts and correlation.ts only separate building attributes from starting or ending the span, so both paths can use them.

Unsure about:

  • With the hand-off, the MCP span is a child of the http.server span instead of its own segment, because forceTransaction can't be applied after the span is started. Is that fine for the MCP insights views?
  • The new optional e2e variant node-express-mcp-v2 (MCP SDK OpenTelemetry) installs the unreleased SDK from pkg.pr.new, pinned to typescript-sdk 919bb8e. It disables block-exotic-subdeps because the previews depend on each other by URL. Once upstream releases, it should switch to npm.

Refs #26078

Verification
  • The new upstreamInstrumentation.test.ts covers hand-off for requests, initialize, and incoming/outgoing notifications. It also covers the fallback when a non-Sentry provider is registered, when no provider is registered, and when the SDK has no instrumentation. The four hand-off tests fail without the fix.
  • The e2e variant node-express-mcp-v2 (MCP SDK OpenTelemetry) passes locally. With only the core change reverted, it fails with two initialize spans.
  • Also passing: the base e2e apps node-express-mcp-v2 and node-express-mcp-v1, the node-integration-tests mcp-server, mcp-server-oauth-client and mcp-handler-exact-once (30 tests), and the core mcp-server + tracing unit tests.

via Francesco Gringl-Novy.

--

View Session [Sentry]

@modelcontextprotocol/server starts its own OpenTelemetry span for every MCP
request and notification once a tracer provider is registered. With the
Sentry tracer provider (e.g. @sentry/nextjs, @sentry/sveltekit, @sentry/deno,
or enableOpenTelemetrySetup: true) every request ended up with two server
spans. The MCP server instrumentation now adds its attributes to the span the
MCP SDK starts and only starts its own span when the SDK records nothing.

Refs #26078

Co-Authored-By: Francesco Gringl-Novy <francesco.novy@sentry.io>
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: 302a18b (PR test merge using develop at 27bc7cb)

Against: 27bc7cb (develop) · base run #65789

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.04 kB - -
@sentry/browser - with treeshaking flags 28.23 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.14 kB - -
@sentry/browser (incl. Tracing) 52.21 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.22 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.17 kB - -
@sentry/browser (incl. Tracing, Replay) 92.75 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.5 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.1 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.52 kB - -
@sentry/browser (incl. Feedback) 47.66 kB - -
@sentry/browser (incl. sendFeedback) 30.61 kB - -
@sentry/browser (incl. FeedbackAsync) 35.63 kB - -
@sentry/browser (incl. Metrics) 31.36 kB - -
@sentry/browser (incl. Logs) 31.63 kB - -
@sentry/browser (incl. Metrics & Logs) 32.29 kB - -
@sentry/react 32.04 kB - -
@sentry/react (incl. Tracing) 54.84 kB - -
@sentry/vue 38.2 kB - -
@sentry/vue (incl. Tracing) 55.12 kB - -
@sentry/svelte 30.06 kB - -
@sentry/remix (Remix 3 client bundle) 57.93 kB - -
CDN Bundle 31.8 kB - -
CDN Bundle (incl. Tracing) 52.55 kB - -
CDN Bundle (incl. Logs, Metrics) 34.58 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.1 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.62 kB - -
CDN Bundle (incl. Tracing, Replay) 90.6 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.22 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 96.01 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.61 kB - -
CDN Bundle - uncompressed 92.67 kB - -
CDN Bundle (incl. Tracing) - uncompressed 155.11 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.01 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 162.75 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 231.09 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 275.26 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 282.89 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 287.05 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 294.68 kB - -
@sentry/nextjs (client) 58.23 kB - -
@sentry/sveltekit (client) 53.24 kB - -
@sentry/core/server 44.91 kB +0.85% +375 B 🔺
@sentry/core/browser 13.99 kB - -
@sentry/node 149.52 kB +0.22% +317 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 83.16 kB - -
@sentry/node - without tracing 88.25 kB -0.01% -6 B 🔽
@sentry/node - without channel injection 138.5 kB +0.23% +317 B 🔺
@sentry/aws-serverless 97.11 kB -0.01% -4 B 🔽
@sentry/cloudflare (withSentry) - minified 204.19 kB - -
@sentry/cloudflare (withSentry) 473.88 kB - -
@sentry/nextjs/cloudflare (withSentry) - minified 229.5 kB - -

View base workflow run

@github-actions

Copy link
Copy Markdown
Contributor

One or more gzipped bundles increased by more than 500 bytes. If this increase is intentional, add the Accept Bundlesize Increase label to this PR to rerun and accept the check.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 5 not successful, 56 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-eve Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E nextjs-16-cacheComponents (cloudflare) Test (optional) Failing after 10m Details
❌ CI: Optional E2E Tests / E2E nestjs-12 (bun) Test (optional) Failing after 48s Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-mastra Test (optional) Successful in 48s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (latest) Test (optional) Successful in 47s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (latest) Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 47s Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 47s Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 38s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (cloudflare) Test (optional) Successful in 38s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 53s Details
✅ CI: Optional E2E Tests / E2E cloudflare-think Test (optional) Successful in 53s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 4m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 32s Details
✅ CI: Optional E2E Tests / E2E nestjs-12 (deno) Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 25s Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-5 (tunnel-dev) Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 (MCP SDK OpenTelemetry) Test (optional) Successful in 30s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 39s Details
✅ CI: Optional E2E Tests / E2E vue-3 (no Options API) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions Test (optional) Successful in 27s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable Test (optional) Successful in 43s Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 37s Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-cloudflare Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 36s Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-openai-decisions (latest) Test (optional) Successful in 34s Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 30s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 41s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details

@mydea mydea self-assigned this Nov 12, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant